﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZJFramework10.Extension;
using TrainingsApp.Model;

namespace TrainingsApp.IPADApp.Master {
    public partial class SchoolEdit : PageBase
    {
     

        public long SchoolId {
            get {
                return ZJFramework10.Utility.QueryStringManager.GetValue<long>("sid");
            }
        }

        public TS_School_Entity EditEntity;

        protected void Page_Load(object sender, EventArgs e) {


            


            this.cbxProvince.DataSource = TrainingsApp.Logic.TS_Province_Logic.TS_Province_ListByPaging(1000, 1).TS_Province_List;
            this.cbxProvince.ValueField = "Idx";
            this.cbxProvince.TextField = "ProvinceName";
            this.cbxProvince.ValueType = typeof(Int32);
            this.cbxProvince.DataBind();

            this.cbxSchoolType.DataSource = TrainingsApp.Logic.TS_SchoolType_Logic.ListAll();
            this.cbxSchoolType.ValueField = "Idx";
            this.cbxSchoolType.TextField = "TypeName";
            this.cbxSchoolType.ValueType = typeof(Int32);
            this.cbxSchoolType.DataBind();


            if (SchoolId > 0) {
                EditEntity = TrainingsApp.Logic.TS_School_Logic.TS_School_View(SchoolId);

                if (!IsPostBack) {

                    if (EditEntity != null) {
                        this.txtAddress.Text = EditEntity.Address;
                        if (EditEntity.SchoolType.HasValue) {
                            this.cbxSchoolType.Value = (int)EditEntity.SchoolType.Value;
                        }
                        this.txtSchoolCode.Text = EditEntity.SchoolCode;
                        this.txtSchoolName.Text = EditEntity.SchoolName;
                        this.txtZipCode.Text = EditEntity.ZipCode;

                        if (this.EditEntity.ProvinceId.HasValue) {
                            this.cbxProvince.Value = this.EditEntity.ProvinceId.Value;
                        }

                        if (this.EditEntity.AreaId.HasValue) {
                            var areaSource = TrainingsApp.Logic.TS_Area_Logic.TS_Area_ListByProvince(EditEntity.ProvinceId.Value);
                            this.cbxArea.DataSource = areaSource;
                            this.cbxArea.ValueField = "Idx";
                            this.cbxArea.ValueType = typeof(Int32);
                            this.cbxArea.TextField = "AreaName";
                            this.cbxArea.DataBind();


                            this.cbxArea.Value = this.EditEntity.AreaId.Value;
                        }

                        if (this.EditEntity.RegionId.HasValue) {

                            var regionSource = TrainingsApp.Logic.TS_Region_Logic.TS_Region_ListByArea(EditEntity.AreaId.Value);
                            this.cbxRegion.DataSource = regionSource;
                            this.cbxRegion.ValueField = "Idx";
                            this.cbxRegion.ValueType = typeof(Int32);
                            this.cbxRegion.TextField = "RegionName";
                            this.cbxRegion.DataBind();

                            this.cbxRegion.Value = this.EditEntity.RegionId.Value;
                        }
                        if (this.EditEntity.TownId.HasValue) {


                            this.cbxTown.DataSource = TrainingsApp.Logic.TS_Town_Logic.TS_Town_ListByRegion(this.EditEntity.RegionId.Value);
                            this.cbxTown.ValueField = "Idx";
                            this.cbxTown.ValueType = typeof(Int32);
                            this.cbxTown.TextField = "TownName";
                            this.cbxTown.DataBind();
                            this.cbxTown.Value = this.EditEntity.TownId.Value;
                        }

                    }
                }
            }
        }



        protected void cbxArea_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e) {
            int provinceId = e.Parameter.AsInt();
            this.cbxArea.DataSource = TrainingsApp.Logic.TS_Area_Logic.TS_Area_ListByProvince(provinceId);
            this.cbxArea.ValueField = "Idx";
            this.cbxArea.ValueType = typeof(Int64);
            this.cbxArea.TextField = "AreaName";

            this.cbxArea.DataBind();
            this.cbxArea.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem("请选择市", -1));
        }

        protected void cbxRegion_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e) {
            int areId = e.Parameter.AsInt();
            this.cbxRegion.DataSource = TrainingsApp.Logic.TS_Region_Logic.TS_Region_ListByArea(areId);
            this.cbxRegion.ValueField = "Idx";
            this.cbxRegion.ValueType = typeof(Int64);
            this.cbxRegion.TextField = "RegionName";
            this.cbxRegion.DataBind();
            this.cbxRegion.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem("请选择区", -1));
        }


        protected void cbxTown_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e) {
            int regionId = e.Parameter.AsInt();
            this.cbxTown.DataSource = TrainingsApp.Logic.TS_Town_Logic.TS_Town_ListByRegion(regionId);
            this.cbxTown.ValueField = "Idx";
            this.cbxTown.ValueType = typeof(Int64);
            this.cbxTown.TextField = "TownName";
            this.cbxTown.DataBind();
            this.cbxTown.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem("请选择乡", -1));
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            bool isUpdate = true;
            if (SchoolId > 0 && EditEntity != null) {

            }
            else {
                isUpdate = false;
                EditEntity = new TS_School_Entity();

            }
            EditEntity.Address = this.txtAddress.Text;
            EditEntity.AreaId = cbxArea.Value.AsInt();
            EditEntity.IsValid = 1;
            EditEntity.ProvinceId = cbxProvince.Value.AsInt();
            EditEntity.RegionId = cbxRegion.Value.AsInt();
            EditEntity.SchoolCode = this.txtSchoolCode.Text;
            EditEntity.SchoolName = this.txtSchoolName.Text;
            EditEntity.SchoolType = this.cbxSchoolType.Value.AsByte();
            EditEntity.TownId = this.cbxTown.Value.AsInt();
            EditEntity.ZipCode = this.txtZipCode.Text;

            if (isUpdate) {
                TrainingsApp.Logic.TS_School_Logic.TS_School_Update(EditEntity);
            }
            else {
                TrainingsApp.Logic.TS_School_Logic.TS_School_Add(EditEntity);
            }

            RedirectTo("SchoolManager.aspx", "保存成功");

        }


        protected void btnBack_Click(object sender, EventArgs e) {

            RedirectTo("SchoolManager.aspx");
        }

    }
}